package com.nativetalk.controller.app.two.member;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberThirdRegister;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.controller.app.two.setmeal.TwoSetmealController;
import com.nativetalk.service.member.login.MemberLoginService;
import com.nativetalk.service.member.register.MemberRegisterService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by lizhun on 16/5/22.
 */
@Controller
public class TwoMemberController {
    private static Logger logger = Logger.getLogger(TwoMemberController.class);
    @Resource
    private MemberLoginService memberLoginService;
    @Resource
    private MemberRegisterService memberRegisterService;

    /**
     * @Purpose  第三方登陆
     * @version  2.1
     * @time     2016-05-22
     * @author   lizhun
     * @param    tdNtMemberThirdRegister
     * @return   RetInfo
     */
    @RequestMapping(value="/app/5/member/third/register" ,method = {RequestMethod.GET,RequestMethod.POST} ,consumes = "application/json")
    @ResponseBody
    public RetInfo register(@Valid @RequestBody TdNtMemberThirdRegister tdNtMemberThirdRegister, BindingResult result,
                            @RequestHeader(value = "client_type", required = true) String client_type,
                            @RequestHeader(value = "version", required = true) Integer version) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors())   {
            List<FieldError> err=result.getFieldErrors();
            FieldError    fe = err.get(0);
            retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
            retInfo.setTip(fe.getDefaultMessage());
        }else {
            try {
                //打印入参
                logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMemberThirdRegister));

                if (version >= 6) {
                    tdNtMemberThirdRegister.setEqu_type(client_type);
                    retInfo = memberRegisterService.thirdregister(tdNtMemberThirdRegister);

                    //打印出参
                    logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                    return retInfo;
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("请更新最新版本");
                }
            } catch (Exception e) {
                //记录错误日志
                retInfo = RetInfo.getServerErrorInfo();
                logger.error(e.getMessage(), e);

                //打印出参
                logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

        }
        return retInfo;
    }
}
